package com.af.community.smart.business

import android.os.Bundle
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.scaleImage
import com.af.community.smart.utils.toFormatString
import com.af.community.smart.view.FullyGridLayoutManager
import com.sunfusheng.GlideImageView
import kotlinx.android.synthetic.main.activity_report_detail.*
import net.idik.lib.slimadapter.SlimAdapter
import net.moyokoo.diooto.Diooto
import net.moyokoo.diooto.config.DiootoConfig

class ReportDetailActivity : BaseActivity() {

    private lateinit var mData: CommonData
    private val imgTop = ArrayList<String>()
    private val imgBottom = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_report_detail)
        initTitle(title = "详情")
    }

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        mData = intent.getSerializableExtra("data") as CommonData

        report_imgs.apply {
            isNestedScrollingEnabled = false
            layoutManager = FullyGridLayoutManager(baseContext, 3)
            adapter = SlimAdapter.create()
                .register<String>(R.layout.item_report_img75) { data, injector ->
                    injector.with<GlideImageView>(R.id.item_report) { it.loadRoundedImage(data.scaleImage()) }
                        .clicked(R.id.item_report) {
                            Diooto(baseContext)
                                .type(DiootoConfig.PHOTO)
                                .urls(imgTop.toTypedArray())
                                .position(imgTop.indexOf(data))
                                .views(report_imgs, R.id.item_report)
                                .loadPhotoBeforeShowBigImage { view, index ->
                                    view.displayImage(imgTop[index].scaleImage())
                                }
                                .start()
                        }
                }
                .attachTo(this)
        }

        report_img2.apply {
            isNestedScrollingEnabled = false
            layoutManager = FullyGridLayoutManager(baseContext, 3)
            adapter = SlimAdapter.create()
                .register<String>(R.layout.item_report_img75) { data, injector ->
                    injector.with<GlideImageView>(R.id.item_report) { it.loadRoundedImage(data.scaleImage()) }
                        .clicked(R.id.item_report) {
                            Diooto(baseContext)
                                .type(DiootoConfig.PHOTO)
                                .urls(imgBottom.toTypedArray())
                                .position(imgBottom.indexOf(data))
                                .views(report_img2, R.id.item_report)
                                .loadPhotoBeforeShowBigImage { view, index ->
                                    view.displayImage(imgBottom[index].scaleImage())
                                }
                                .start()
                        }
                }
                .attachTo(this)
        }

        report_title.text =
            if (mData.gridName.isNullOrEmpty()) "${mData.eventType}/${mData.eventTitle}"
            else "[${mData.gridName}] ${mData.eventType}/${mData.eventTitle}"
        report_time.text = mData.createDate.toFormatString()
        report_content.text = mData.eventDetail

        if (!mData.remark.isNullOrEmpty()) {
            imgTop.addAll(mData.remark!!.split(","))
            (report_imgs.adapter as SlimAdapter).updateData(imgTop)
        }

        when (mData.state) {
            "1" -> {
                report_ll.gone()
                report_img2.gone()
            }
            "2" -> {
                report_result.visible()
                report_ll.visible()

                report_name.text = mData.solveUserName
                report_ctime.text = mData.solveDate.toFormatString()
                report_result.text = mData.solveContent
                val picture = mData.solvePic ?: ""
                if (picture.isNotEmpty()) {
                    imgBottom.addAll(picture.split(","))
                    (report_img2.adapter as SlimAdapter).updateData(imgBottom)
                }
            }
        }
    }

}
